package buildrobot_info

import (
	"go_server/game_server/api/service/build"
	"go_server/game_server/api/service/build_resource"
	"go_server/game_server/constants"
	"go_server/game_server/dataconfig/config_manager"
	"go_server/game_server/protocol/pb"
	"go_server/game_server/util"
	"go_server/zinx/ziface"
	"go_server/zinx/zlog"
	"math"
)

func (s *Buildrobot_info_service) S2C_CheckBuildRobotInfoByBuildUnlock(conn ziface.IConnection, roleid int64) pb.ErrorCodeManager {
	robot := s.LoadByRoleId(roleid)
	if robot != nil {
		maxcount := int32(0)
		transvolume := int32(0)

		bids := config_manager.GetTableBuildSheetBuildConfigIds(constants.Build_Info_WorkerStation) //配置表中所有类型是人工驿站的建筑配置id
		if builds, errCode := build.GetRoleBuildPosInfo(roleid); errCode == pb.ErrorCodeManager_SUCCESS {
			//角色拥有的所有建筑
			for _, mv := range builds { //mapid
				for cfgid, cm := range mv {
					if bids[cfgid] {
						for _, sm := range cm { //serid
							lv := sm[2] //建筑等级
							cfgBL := config_manager.GetTableBuildSheetLevelConfig(cfgid, lv)
							if &cfgBL != nil {
								if len(cfgBL.Effect) > 0 {
									mr := int32(cfgBL.Effect[0]) //最大机器人数量
									maxcount = int32(math.Max(float64(maxcount), float64(mr)))
								}

								if len(cfgBL.Effect) > 1 {
									mr := int32(cfgBL.Effect[1]) //运输量
									transvolume = int32(math.Max(float64(transvolume), float64(mr)))
								}
							}
						}
					}
				}
			}
		}

		robot.MaxCount = maxcount
		robot.TransVolume = transvolume

		rtlen := len(robot.RobotData)
		difflen := int(maxcount) - rtlen

		if difflen > 0 { //需要添加新的机器人
			robot.CreateNewRobot(difflen)
		}

		defer func() {
			robot.Save_Robot()
		}()
	}
	return pb.ErrorCodeManager_SUCCESS
}

func (s *Buildrobot_info_service) S2C_GetAllBuildRobots(conn ziface.IConnection, roleid int64, msg *pb.GetAllBuildRobotsC2S) pb.ErrorCodeManager {
	robot := s.LoadByRoleId(roleid)
	msgS2C := &pb.GetAllBuildRobotsS2C{}
	if robot != nil {

		buildRobots := util.NewSet[int32]() //在建造队列中的机器人

		if robot.BuildData != nil && len(robot.BuildData) > 0 {

			lvupBuilds := make([]*pb.BuildRobotLvUpData, 0)
			for _, bv := range robot.BuildData {
				//升级中的建筑
				nb := bv.ToPb_BuildRobotLvUpData(robot.GetRobotBySerid)
				lvupBuilds = append(lvupBuilds, nb)
				buildRobots.AddValue(bv.RobotSerids)
			}
			msgS2C.BuildOneof = &pb.GetAllBuildRobotsS2C_Lvup{
				Lvup: &pb.GetAllBuildRobotsS2C_BuildLvUpData{
					Builds: lvupBuilds,
				},
			}
		}

		if robot.RobotData != nil && buildRobots.Size() > 0 {
			for id, v := range robot.RobotData {
				if !buildRobots.Contains(id) {
					v.Trans2Idle() //不在建造中的机器人进入空闲状态
				}
			}
		}
		msgS2C.IdleRobots = robot.Pb_GetIdleRobot()

		// 获取正在采集中的机器人
		buildResourceCollectRobots := make(map[int32]int32)
		if robot.BuildResourceData != nil && len(robot.BuildResourceData) > 0 {
			for buildId, buildInfo := range robot.BuildResourceData {
				for robotId, robotInfo := range buildInfo {
					if robotInfo[0] == build_resource.START_COLLECT_RESOURCE { // 正在采集中
						buildResourceCollectRobots[robotId] = buildId
					}
				}
			}
		}
		msgS2C.BuildResourceCollectRobots = buildResourceCollectRobots
	}
	if msgS2C != nil {
		zlog.Debugf("ProtocolManager_GetAllBuildRobots %v", msgS2C)
		defer func() {
			conn.SendBuffPBMsg(uint32(pb.ProtocolManager_GetAllBuildRobots), msgS2C)
		}()
	}
	return pb.ErrorCodeManager_SUCCESS
}
